define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/report/models'),
        utils = require('js/utils/tools'),
        chart = require('js/utils/chart'),
        helper = require('js/apps/report/common'),
        tz = require('js/utils/tz');

    var ShippingView = Backbone.View.extend({
        events: {
            'click #search': 'doSearch',
            'click #refresh': 'refreshData',
        },
        initialize: function (options) {
            this.options = options;
            this.model = new models.ShippingReport();
            this.model.bind('change reset', this.renderWithData, this);
        },
        doSearch: function () {
            this.options.dateRange = helper.getDateRange();
            this.load();
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        refreshData: function () {
            var option = {
                    date_range: helper.getDateRange().join(',')
                },
                that = this;
            $.post(this.model.url, JSON.stringify(option)).then(function () {
                notify.success();
                that.load();
            }).fail(function (resp) {
                notify.notifyResp(resp);
            })
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.model.list,
                info: this.model.info,
                options: this.options
            }));
            helper.loadDatePicker(this.options);
            return this;
        },
        load: function () {
            this.model.fetch({
                reset: true,
                data: {
                    date_range: this.options.dateRange.join(','),
                },
                error: function (c, r, o) {
                    notify.notifyResp(r);
                }
            })
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "report/Shipping.html",
        render: function () {
            var yesterday = tz.yesterday(),
                dateRange = [yesterday, yesterday],
                view = new ShippingView({
                    dateRange: dateRange
                });
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();
            return this;
        }
    });

});